'use strict';
const db = uniCloud.database();

exports.main = async (event, context) => {
  try {
    // 简化数据解构，只获取必要字段
    const {imageList, uid, userid, content, contact = '', type = 'suggestion' } = event;
    
    // 验证必填字段
    if (!content || content.trim().length === 0) {
      return {
        code: 400,
        message: '反馈内容不能为空'
      };
    }
    
    // 获取用户ID（从context中获取）
    let userId = context.uid || userid;
    console.log('用户ID:', userId);
    
    // 准备反馈数据
    const feedbackData = {
		imageList,
		uid,
		userid,
      content: content.trim(),
      contact: contact.trim(),
      type: type,
      userId: userId,
      createTime: new Date(),
      status: 'pending'
    };
    
    console.log('准备保存的反馈数据:', feedbackData);
    
    // 保存到数据库
    const result = await db.collection('feedbacks').add(feedbackData);
    
    console.log('反馈保存成功，ID:', result.id);
    
    return {
      code: 0,
      message: '反馈提交成功',
      data: {
        feedbackId: result.id
      }
    };
  } catch (error) {
    console.error('提交反馈失败:', error);
    return {
      code: 500,
      message: '服务器错误，请稍后重试',
      errorDetail: error.message || String(error)
    };
  }
};